package inf3791.calendarsystem.others.panels.calendar;

import inf3791.calendarsystem.shared.Appointment;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

/**
 *
 * @author Jonas Weber
 */
class ListTableModel implements TableModel {

    private List<TableModelListener> listeners = new LinkedList<>();
    private List<Appointment> appointments;

    public List<Appointment> getAppointments() {
        return appointments;
    }

    public void setAppointments(List<Appointment> appointments) {
        this.appointments = appointments;
        TableModelEvent tme = new TableModelEvent(this);
        for (TableModelListener l : listeners) {
            l.tableChanged(tme);
        }
    }

    @Override
    public int getRowCount() {
        if (appointments != null) {
            return appointments.size();
        } else {
            return 0;
        }
    }

    @Override
    public int getColumnCount() {
        return 1;
    }

    @Override
    public String getColumnName(int i) {
        return "asd";
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return Appointment.class;
    }

    @Override
    public boolean isCellEditable(int i, int i1) {
        return true;
    }

    @Override
    public Object getValueAt(int i, int i1) {
        if (appointments != null) {
            return appointments.get(i);
        } else {
            return null;
        }
    }

    @Override
    public void setValueAt(Object o, int row, int col) {
        if (appointments != null)
        {
            appointments.set(row, (Appointment) o);
        }
    }

    @Override
    public void addTableModelListener(TableModelListener tl) {
        listeners.add(tl);
    }

    @Override
    public void removeTableModelListener(TableModelListener tl) {
        listeners.remove(tl);
    }
}
